local qiudao = fk.CreateSkill{
  name = "xiaobai__qiudao",
}

Fk:loadTranslationTable{
  ["xiaobai__qiudao"] = "求道",
  [":xiaobai__qiudao"] = "没有字母牌被使用过的回合结束时，你可以观看牌堆顶至多五张牌，然后使用其中最后一张牌，若你以此法使用了字母牌，则你获得"..
  "其余牌，并可将其中任意张置于牌堆底。",

  ["#xiaobai__qiudao-choice"] = "求道：你可以观看牌堆顶至多五张牌，使用最后一张牌",
  ["#xiaobai__qiudao-use"] = "求道：你可以使用最后一张牌",

  ["$xiaobai__qiudao1"] = "见心明性，念念回首，即是灵山。",
  ["$xiaobai__qiudao2"] = "法本心生，还从心灭，生灭由谁？",
}

qiudao:addEffect(fk.TurnEnd, {
  anim_type = "drawcard",
  can_trigger = function(self, event, target, player, data)
    return player:hasSkill(self) and
      #player.room.logic:getEventsOfScope(GameEvent.UseCard, 1, function (e)
        return table.contains({1, 11, 12, 13}, e.data.card.number)
      end, Player.HistoryTurn) == 0
  end,
  on_cost = function (self, event, target, player, data)
    local choice = player.room:askToChoice(player, {
      choices = {"1", "2", "3", "4", "5", "Cancel"},
      skill_name = qiudao.name,
      prompt = "#xiaobai__qiudao-choice"})
    if choice ~= "Cancel" then
      event:setCostData(self, {choice = tonumber(choice)})
      return true
    end
  end,
  on_use = function (self, event, target, player, data)
    local room = player.room
    local cards = table.simpleClone(room:getNCards(event:getCostData(self).choice))
    local use =  room:askToUseRealCard(player,{ 
      pattern = {cards[#cards]},
      expand_pile = {cards[#cards]},
      skill_name = qiudao.name,
      prompt = "#xiaobai__qiudao-use",
      extra_data = {
        bypass_times = true,
        extraUse = true,
        expand_pile = cards,
      },
      cancelable = true,
      skip = true})
    if use then
      table.remove(cards, #cards)
    else
      return
    end
    room:useCard(use)
    if player.dead or not table.contains({1, 11, 12, 13}, use.card.number) then return end
    cards = table.filter(cards, function (id)
      return table.contains(room.draw_pile, id)
    end)
    if #cards == 0 then return end
    room:moveCardTo(cards, Card.PlayerHand, player, fk.ReasonJustMove, qiudao.name, nil, false, player)
    cards = table.filter(cards, function (id)
      return table.contains(player:getCardIds("h"), id)
    end)
    if #cards == 0 or player.dead then return end
    local result = room:askToGuanxing(player, {
      cards = cards, 
      skill_name = qiudao.name,
      skip = true,
      area_names = {"hand_card", "Bottom"}})
    if #result.bottom > 0 then
      room:moveCards{
        ids = table.reverse(result.bottom),
        from = player,
        toArea = Card.DrawPile,
        moveReason = fk.ReasonJustMove,
        skillName = qiudao.name,
        drawPilePosition = -1,
        moveVisible = false,
      }
    end
  end,
})

return qiudao